package com.kaviddiss.bootquartz.thread;

import com.kaviddiss.bootquartz.job.SampleJob;
import com.kaviddiss.bootquartz.service.SampleService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Created by Studio on 2017/5/19.
 *
 */
@Component
public class Test2Thread extends Thread{

    @Autowired
    private Scheduler scheduler;

    private static final Logger LOG = LoggerFactory.getLogger(SampleService.class);

    private static Test2Thread testThread;

    public Test2Thread() {
    }

    @PostConstruct     //关键二   通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
    public void init() {
        testThread = this;
        testThread.scheduler = this.scheduler;
    }

    @Override
    public void run ()  {
        Class myclass = null;
        try {
            String className = "com.kaviddiss.bootquartz.job.SampleJob";
            myclass = Class.forName(className);

            String jobName = "test";
            String jobName1 = "test1";
            String jobGroupName = "testGroup";

            String triggerName = "testTri";
            String triggerName1 = "testTri1";
            String triggerGroupName = "testTriGroup";
            String cron = "*/1 * * * * ?";

            JobDetail jobDetail = JobBuilder.newJob(myclass).withIdentity(jobName, jobGroupName).build();
            JobDetail jobDetail1 = JobBuilder.newJob(myclass).withIdentity(jobName1, jobGroupName).build();
            JobKey jobKey= jobDetail.getKey();
            LOG.info(jobKey.getName());
            LOG.info(jobKey.getGroup());
            jobDetail.getJobDataMap().put("Test", "This is test value");
            jobDetail1.getJobDataMap().put("Test", "This is the second value");
            // 触发器
            TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
            // 触发器名,触发器组,触发器时间设定
            triggerBuilder.withIdentity(triggerName1, triggerGroupName).startNow().withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            CronTrigger trigger = (CronTrigger) triggerBuilder.build();

            // 触发器
            TriggerBuilder<Trigger> triggerBuilder1 = TriggerBuilder.newTrigger();
            // 触发器名,触发器组,触发器时间设定
            triggerBuilder1.withIdentity(triggerName, triggerGroupName).startNow().withSchedule(CronScheduleBuilder.cronSchedule(cron));
            // 创建Trigger对象
            CronTrigger trigger1 = (CronTrigger) triggerBuilder1.build();

            // 调度容器设置JobDetail和Trigger
            testThread.scheduler.scheduleJob(jobDetail, trigger);
            testThread.scheduler.scheduleJob(jobDetail1, trigger1);
            // jobDetail.setDurability(true);


            Thread.sleep(5000);
            if (testThread.scheduler.isStarted()) {
                LOG.info(testThread.scheduler.getJobGroupNames().get(0));

                testThread.scheduler.deleteJob(jobDetail1.getKey());
                testThread.scheduler.deleteJob(jobDetail.getKey());
                LOG.info("删除");
        //            testThread.scheduler.deleteJob();
                LOG.info("关闭定时器");
//                testThread.scheduler.shutdown();
            }

        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
